﻿using PMIS.Events;
using Prism.Events;
using Prism.Mvvm;
using Prism.Commands;
using PMIS.Models;

namespace PMIS.ViewModels
{
    public class HeaderViewModel : BindableBase
    {
        private readonly IEventAggregator _eventAggregator;

        public HeaderViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
        }

        private AccountInfo _userInfo;

        public AccountInfo UserInfo
        {
            get
            {
                return _userInfo;
            }
            set
            {
                SetProperty(ref _userInfo, value);
            }
        }

        private DelegateCommand logoutCommand;

        public DelegateCommand LogoutCommand
        {
            get
            {
                if (logoutCommand == null)
                {
                    logoutCommand = new DelegateCommand(OnLogout);
                }
                return logoutCommand;
            }
        }


        private DelegateCommand loadedCommand;

        public DelegateCommand LoadedCommand
        {
            get
            {
                if (loadedCommand == null)
                {
                    loadedCommand = new DelegateCommand(Loaded);
                }
                return loadedCommand;
            }
        }

        private void Loaded()
        {
            InitInfo();
        }

        private void InitInfo()
        {
            this.UserInfo = AccountInfo.Instance;
        }

        private void OnLogout()
        {
            _eventAggregator.GetEvent<LogoutEvent>().Publish();
        }
    }
}
